package pers.jason.wppm.edge.web.security.userdetails;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Service;

import pers.jason.wppm.auth.api.entity.User;
import pers.jason.wppm.auth.api.UserApi;

/**
 * 
 * @author Jason
 * @Date 2018-10-29 11:03
 *
 */
@Service
public class DefaultJdbcUserDetails implements UserDetailsService, SocialUserDetailsService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserApi userApi;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		User user = userApi.getUserByLoginName("_MOBILE", username);
		logger.info("用户名：" + username + "；查询的用户：" + user.toString());
		return new org.springframework.security.core.userdetails.User(username, user.getPassword(), user.getIsEnable(), user.getIsAccountNonExpired(), user.getIsCredentialsNonExpired(), user.getIsAccountNonLocked(), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
	}

	@Override
	public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
		return new SocialUser(userId, (userId+"123"), true, true, true, true, AuthorityUtils.commaSeparatedStringToAuthorityList("admin, ROLE_USER"));
	}
}
